import { login, logout, getInfo } from '@/api/system/user.js'
import { getLesseeInfo } from '@/api/system/lessee.js'
import { getToken, setToken, removeToken } from '@/utils/auth'
import md5 from 'js-md5'
const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    role: '',
    permissions: [],
    userModel: {},
    lessee: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USER: (state, userModel) => {
      state.userModel = userModel
    },
    SET_USERNAME: (state, username) => {
      state.username = username
    },
    SET_ROLE: (state, role) => {
      state.role = role
    },
    SET_LESSEE: (state, lessee) => {
      state.lessee = lessee
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password.trim()
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login({'username': username, 'password': md5(password), 'code': code, 'uuid': uuid}).then(res => {
          var token = res.data.data.token
          setToken(token)
          commit('SET_TOKEN', token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    SetUser({ commit, state }, tokenInfo){
      var token = tokenInfo.key
      setToken(token)
      commit('SET_TOKEN', token)

      const user = tokenInfo.userModel
      commit('SET_USER', user)
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.data.data
          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : user.avatarUrl;
          commit('SET_NAME', user.username)
          commit('SET_AVATAR', avatar)
          commit('SET_USER', user)
          commit('SET_ROLE', user.role)
          getLesseeInfo().then(res => {
            if(res.data.code == 10000){
              commit('SET_LESSEE', res.data.data)
            }
          })
          if (user.username=='lessee'){
            localStorage.setItem('changeLesseeStatus', 'Y')
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout({token: state.token}).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_NAME', '')
          commit('SET_USER', {})
          removeToken()
          localStorage.removeItem('changeLesseeStatus')
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_NAME', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
